//build: 20240525
//Dependency:dxCommon
import { sqliteClass } from './libvbar-m-dxsqlite.so'
import dxCommon from './dxCommon.js'
const sqliteObj = new sqliteClass();
const sqlite = {}

/**
 * Initialize the database
 * @param {string} path Full path to the .db file (required)
 * @param {string} id Handle ID, optional (a unique ID is required if initializing multiple instances).
 */
sqlite.init = function (path, id) {
    if (path == undefined || path.length == 0) {
        throw new Error("dxsqliteObj.initDb:path should not be null or empty")
    }
    let pointer = sqliteObj.open(path);
    dxCommon.handleId("sqlite", id, pointer)
}

/**
 * Execution statement
 * @param {string} sql Script statement, required
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns 0: Success, non-zero: Failure
 */
sqlite.exec = function (sql, id) {
    let pointer = dxCommon.handleId("sqlite", id)
    return sqliteObj.sql_exec(pointer, sql)
}


/**
 * Execute query statement
 * @param {string} sql Script statement, required
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns The query results are in the form of：[{"id":"1","type":200,"code":"aad7485a","door":"entrance","extra":"","tiemType":0,"beginTime":1716613766,"endTime":1716613766,"repeatBeginTime":1716613766,"repeatEndTime":1716613766,"period":"extra"}]
 */
sqlite.select = function (sql, id) {
    let pointer = dxCommon.handleId("sqlite", id)
    return sqliteObj.select(pointer, sql);
}

/**
 * Close database connection
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns 0: Success, non-zero: Failure
 */
sqlite.close = function (id) {
    let pointer = dxCommon.handleId("sqlite", id)
    return sqliteObj.close(pointer)
}

export default sqlite;